<?php
/**
 * <https://y.st./>
 * Copyright (C) 2015-2016 //y.st. <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org./licenses/>.
**/
use st\y\calendar;

foreach(scandir(__DIR__.'/pages/en/weblog') as $year):
	if($year != '.' and $year != '..'):
		$calendar = array();
		foreach($months as $monthnum => $monthdir):
			if(is_dir(__DIR__."/pages/en/weblog/$year/$monthdir")):
				$calendar[$monthnum] = <<<END
<pre>/--------------------\\
|  $year   {$monthheads[$monthnum]}  |
END;
				foreach(new calendar($year, $monthnum) as $week):
					$calendar[$monthnum] .= "\n|--+--+--+--+--+--+--|\n|";
					foreach($week as $datetime):
						$date = $datetime->format('Y/m-F/d');
						$key_year = $datetime->format('Y');
						$key_month = $datetime->format('m');
						$key_day = $datetime->format('d');
						$date_linktext = $entry($key_year, $key_month, $key_day);
						$date_linktext_li = substr($date_linktext, 11);
// Needed because Links is broken.
						$date_li_value = substr($date_linktext, 4, 5);
						if(file_exists(__DIR__."/pages/en/weblog/$date.xhtml") and $datetime->format('n') == $monthnum):
// The graphical calendar day:
							$calendar[$monthnum] .= "<a href=\"/en/weblog/$date.xhtml\" title=\"$date_linktext\">$key_day</a>|";
// The list-based calendar day:
							$ol_array[$key_year][$key_month][$key_day] = "<li value=\"$date_li_value\"><a href=\"/en/weblog/$date.xhtml\">$date_linktext_li</a></li>";
// We need to know what entry comes first:
/**
 * This is a better way to handle list item numbers than using the
 * "value" attribute on every last <li/> tag, but the Links Web browser
 * is broken. It cannot handle starting from zero and cannot handle
 * reversed lists at all.
**/
#							if(!isset($ol_start[$key_year][$key_month])):
#								$ol_start[$key_year][$key_month] = substr($date_linktext, 4, 5);
#							endif;
// We also need to know which entry comes last:
#							$ol_end[$key_year][$key_month] = substr($date_linktext, 4, 5);
// Set up RSS entries:
							$RSS_entry[] = array(
// We don't actually know the title yet until we require the entry file.
#								'title' => '',
								'path'  => $date,
								'date'  => $datetime->format(DateTime::RSS),
							);
						else:
							$calendar[$monthnum] .= '  |';
						endif;
					endforeach;
				endforeach;
				$calendar[$monthnum] .= "\n\\--------------------/</pre>";

				$¢_build_page("en/weblog/$year/$monthdir/index.xhtml", array(
					'title' => "Weblog: $year ".substr($monthdir, 3),
					'copyright year' => $year,
					'body' => '<div class="weblogindex">'.$calendar[$monthnum].'</div>',
				));
			endif;
		endforeach;
		$yearcal[$year] = implode("\n", $calendar);
		$¢_build_page("en/weblog/$year/index.xhtml", array(
			'title' => "Weblog: $year",
			'copyright year' => $year,
			'body' => '<div class="weblogindex">'.$yearcal[$year].'</div>',
		));
	endif;
endforeach;

// The semi-graphical weblog index:
$¢_build_page('en/weblog/index.xhtml', array(
	'title' => "Weblog",
	'copyright year' => "2015-$latest_copyright_year",
	'body' => '<div class="weblogindex">'.implode("\n", $yearcal).'</div>',
));

// The list-based weblog indexes:
$month_list_ascending = array();
$month_list_descending = array();
foreach($ol_array as $key_year => $year):
	foreach($year as $key_month => $month):
// Ascending
#		$start = $ol_start[$key_year][$key_month];
		$list = implode("\n", $month);
// Links is broken and cannot handle this if start="0".
#		$month_list_ascending[] = "<ol start=\"$start\">\n$list\n</ol>";
		$month_list_ascending[] = "<ol>\n$list\n</ol>";
// Descending
#		$start = $ol_end[$key_year][$key_month];
		$list = implode("\n", array_reverse($month));
// Links is broken and cannot handle revered lists.
#		$month_list_descending[] = "<ol start=\"$start\" reversed=\"reversed\">\n$list\n</ol>";
		$month_list_descending[] = "<ol>\n$list\n</ol>";
	endforeach;
endforeach;
$list_ascending = implode("\n\n", $month_list_ascending);
$list_descending = implode("\n\n", array_reverse($month_list_descending));

// Ascending
$¢_build_page('en/weblog/index_ol_ascending.xhtml', array(
	'title' => 'Weblog',
	'copyright year' => "2015-$latest_copyright_year",
	'body' => $list_ascending,
));

// Descending
$¢_build_page('en/weblog/index_ol_descending.xhtml', array(
	'title' => 'Weblog',
	'copyright year' => "2015-$latest_copyright_year",
	'body' => $list_descending,
));

// The latest weblog entry should always be available on this page:
copy(__DIR__."/../compiled$latest_entry[day]", __DIR__.'/../compiled/en/weblog/latest.xhtml');
$should_exist['en/weblog/latest.xhtml'] = true;

// The latest weblog month index should always be available on this page:
copy(__DIR__."/../compiled$latest_entry[month]/index.xhtml", __DIR__.'/../compiled/en/weblog/latest_month.xhtml');
$should_exist['en/weblog/latest_month.xhtml'] = true;

// The latest weblog year index should always be available on this page:
copy(__DIR__."/../compiled$latest_entry[year]/index.xhtml", __DIR__.'/../compiled/en/weblog/latest_year.xhtml');
$should_exist['en/weblog/latest_year.xhtml'] = true;

/*
// RSS feed (I need more information to complete this. What is the correct XML namespace to use?):
$build_date = new DateTime();
$RSS_feed = <<<END
<?xml version="1.0" encoding="utf-8"?>
<!--
	<https://y.st./>
	Copyright (C) 2015-2016 //y.st. <mailto:copyright@y.st>

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org./licenses/>.
-->
<rss version="2.0">
	<channel>
		<title>Yst Dawson&apos;s weblog</title>
		<description>My daily journal</description>
		<link>https://authorednansyxlu.onion/en/weblog/</link>
		<lastBuildDate>{$build_date->format(DateTime::RSS)}</lastBuildDate>
		<pubDate>Wed, 09 Mar 2016 15:35:57 -0800</pubDate>
		<ttl>1800</ttl>

END;
foreach($RSS_entry as $RSS_item):
	require __DIR__."/pages/en/weblog/$RSS_item[path].xhtml";
	$RSS_feed .= <<<END
		<item>
			<title>$xhtml[title]</title>
			<!-- description>Not implemented</description -->
			<link>https://authorednansyxlu.onion/en/weblog/$RSS_item[path].xhtml</link>
			<guid isPermaLink="true">https://authorednansyxlu.onion/en/weblog/$RSS_item[path].xhtml</guid>
			<pubDate>$RSS_item[date]</pubDate>
		</item>

END;
endforeach;
$RSS_feed .= "\t</channel>\n</rss>\n";
*/
